﻿using Lightworks.Core.Constant;
using Lightworks.System.Models;
using Lightworks.System.Services;
using Microsoft.AspNetCore.Mvc;

namespace Lightworks.Api.Controllers
{
    [ApiController]
    [Route("api/system/menu")]
    public class RescoureController : ApiBaseController
    {
        IResourceService resourceService;
        ILogger<RescoureController> logger;
        public RescoureController(IResourceService resourceService, ILogger<RescoureController> logger) 
        {
            this.logger = logger;
            this.resourceService = resourceService;
        }
       
        [HttpGet("list")]
        public ApiResult List() 
        {
            var data = resourceService.GetAll();
            return Ok(data:data);
        }

        [HttpPost("delete")]
        public ApiResult Delete(long[] ids) 
        {
            try
            {
                if (!resourceService.Delete(ids))
                {
                    return Error("删除失败");
                }

            }
            catch (Exception ex)
            {
                logger.LogError(ex,$"delete user err. ids:{ids}");
                return Error($"Failed to delete {ex.Message}");
            }
            return Ok();
        }

        [HttpPost("save")]
        public ApiResult Save(Resource res) 
        {
            long id = 0;
            try
            {
                id = resourceService.Save(res);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"delete user err. obj:{res}");
                return Error($"Failed to delete {ex.Message}");
            }
            return Ok(data:id);
        }

        [HttpGet("my")]
        public ApiResult My() 
        {
            var data =resourceService.GetResourceByUser(HttpContext.User.Identity!.Name!);
            return Ok(data:data);
        }
       
    }
}
